/**
 * Copyright 中国电信甘肃万维公司 All rights reserved.
 * 中国电信甘肃万维公司 专有/保密源代码,未经许可禁止任何人通过任何* 渠道使用、修改源代码.
 */
package com.gsww.vue.criteria;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.util.StringUtils;

/**
 * 简单条件表达式
 * @author lee
 *
 */
public class SimpleExpression implements Criterion {
	/** 属性名 */
	private String fieldName; 
	/** 对应值 */
	private Object value; 
	/** 计算符 */
	private Operator operator; 

	protected SimpleExpression(String fieldName, Object value, Operator operator) {
		this.fieldName = fieldName;
		this.value = value;
		this.operator = operator;
	}

	public String getFieldName() {
		return fieldName;
	}

	public Object getValue() {
		return value;
	}

	public Operator getOperator() {
		return operator;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Predicate toPredicate(Root<?> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
		Path expression = null;
		String point = ".";
		if (fieldName.contains(point)) {
			String[] names = StringUtils.split(fieldName, ".");
			expression = root.get(names[0]);
			for (int i = 1; i < names.length; i++) {
				expression = expression.get(names[i]);
			}
		} else {
			expression = root.get(fieldName);
		}

		switch (operator) {
			case EQ:
				return builder.equal(expression, value);
			case NE:
				return builder.notEqual(expression, value);
			case LIKE:
				return builder.like((Expression<String>) expression, "%" + value + "%");
			case LT:
				return builder.lessThan(expression, (Comparable) value);
			case GT:
				return builder.greaterThan(expression, (Comparable) value);
			case LTE:
				return builder.lessThanOrEqualTo(expression, (Comparable) value);
			case GTE:
				return builder.greaterThanOrEqualTo(expression, (Comparable) value);
			default:
				return null;
		}
	}

}
